package com.college_career_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.college_career_system.dao.SubmitRecruitMapper;
import com.college_career_system.dao.UserMapper;
import com.college_career_system.pojo.dto.EnterpriseDTO;
import com.college_career_system.pojo.po.Enterprise;
import com.college_career_system.dao.EnterpriseMapper;
import com.college_career_system.pojo.po.Recruit;
import com.college_career_system.pojo.po.SubmitRecruit;
import com.college_career_system.pojo.po.User;
import com.college_career_system.service.EnterpriseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college_career_system.utils.*;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Empty
 * @since 2022-12-04
 */
@Service
@Slf4j
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements EnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private SubmitRecruitMapper submitRecruitMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject EnterpriseRegister(EnterpriseDTO enterpriseDTO) {

        if(StringUtil.hasOneEmptyOrNull(enterpriseDTO.getEnterpriseName(), enterpriseDTO.getEnterpriseIntroduce(),
                enterpriseDTO.getEtypeName(), enterpriseDTO.getTelephone())) {
            return ResponseResult.failure("资料请填写完整！", ResponseCode.STATUS_ERROR);
        }
        Enterprise enterprise = DtoPoMapper.INSTANCE.toEnterprise(enterpriseDTO);
        SnowFlakeWorkerUtil snowFlakeWorkerUtil = new SnowFlakeWorkerUtil(0, 0);
        long id = snowFlakeWorkerUtil.nextId();
        enterprise.setEnterpriseId(id);
        enterpriseMapper.insert(enterprise);
        return ResponseResult.success(String.valueOf(id), ResponseCode.STATUS_SUCCESS);
    }

    @Override
    public JSONObject EnterpriseLogin(String account, String psw) {

        if(StringUtil.hasOneEmptyOrNull(account, psw)) {
            return ResponseResult.failure("信息填写不完整！", ResponseCode.STATUS_ERROR);
        }
        QueryWrapper<Enterprise> wrapper = new QueryWrapper<>();
        wrapper.eq("enterprise_id", Long.valueOf(account));
        Enterprise enterprise = enterpriseMapper.selectOne(wrapper);
        if(enterprise == null) {
            return ResponseResult.failure("账号信息错误！", ResponseCode.STATUS_ERROR);
        }
        if(psw.equals(enterprise.getPwd())) {
            return ResponseResult.success();
        }
        return ResponseResult.failure("登入失败！", ResponseCode.STATUS_ERROR);
    }

    @Override
    public JSONObject selectReceivedRecruit(HttpServletRequest request, int flag) {

        // 返回用户名 + 用户投递的岗位类型
        return ResponseResult.success(submitRecruitMapper.selectSubmit(TokenUtil.getId(request), flag));
    }

    @Transactional
    @Override
    public JSONObject updateState(int hid, int flag, String uid) {

        // 判断是否已经就业
        User user1 = userMapper.selectById(uid);
        if(user1.getEmploymentOrNot() != 0) {
            return ResponseResult.failure("已被录用！", ResponseCode.STATUS_ERROR);
        }


        // 修改投简历的状态
        SubmitRecruit submitRecruit = new SubmitRecruit();
//        submitRecruit.setHid(hid);
        submitRecruit.setWhetherAgree(flag);
        final QueryWrapper<SubmitRecruit> wrapper = new QueryWrapper<>();
        wrapper.eq("hid",hid);
        submitRecruitMapper.changeWhetherAgree(hid, flag);
        System.out.println(submitRecruit.getSubmitTime());
        if(flag == 1) {

            SubmitRecruit submitRecruit1 = submitRecruitMapper.selectById(hid);
            // 修改用户当前为就业状态
            User user = new User();
            user.setUid(uid).setRecruitId(submitRecruit1.getRecruitId()).setEmploymentOrNot(1);
            userMapper.updateById(user);
            return ResponseResult.success();
        }
        return ResponseResult.failure(ResponseCode.STATUS_ERROR);
    }

    @Override
    public JSONObject updatePassWordById(HttpServletRequest request, String pwd) {
        if (StringUtil.isEmptyOrNull(pwd)){
            return ResponseResult.failure("传入密码为空", ResponseCode.STATUS_ERROR);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if (enterpriseMapper.updatePassWordById(TokenUtil.getId(request),bCryptPasswordEncoder.encode(pwd)) > 0){
            return ResponseResult.success("更新成功");
        }
        return ResponseResult.failure("更新失败",ResponseCode.STATUS_ERROR);
    }

}
